package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.Channel;
import com.elinkway.infinitemovies.bean.ChannelNewBean;
import com.elinkway.infinitemovies.bean.ChannelNewParams;
import com.elinkway.infinitemovies.bean.ChannelPiece;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by zhaopeipei on 2016/9/12.
 */
public class ChannelPieceParser extends LVideoMobileParser<Channel>  {

    private static final String ID = "id";

    private static final String NAME = "name";

    private static final String SORT = "sort";

    private static final String PIC = "pic";

    private static final String CHANNELS = "channels";

    private static final String ICON = "icon";

    private static final String TYPE = "type";

    private static final String PARAM = "param";

    private static final String VT = "vt";

    private static final String TITLE = "title";

    private static final String SUBCATEGORY = "subcategory";

    private static final String AREA = "area";

    private static final String YEAR = "year";

    private static final String ORDERBY = "orderby";

    private static final String THEME_ID = "theme_id";

    private static final String AID = "aid";

    private static final String URL = "url";

    private JSONArray mArr;

    @Override
    public Channel parse(JSONObject data) throws Exception {
        Channel channel = new Channel();
        ArrayList<ChannelPiece> channelPieceArrayList = new ArrayList();
        for (int i = 0; i < mArr.length(); i++) {
            ChannelPiece channelPiece = new ChannelPiece();
            JSONObject jsonObject = mArr.getJSONObject(i);
            channelPiece.setName(jsonObject.optString(NAME));
            channelPiece.setSort(jsonObject.optString(SORT));
            channelPiece.setPic(jsonObject.optString(PIC));
            channelPiece.setVt(jsonObject.optString(VT));
            JSONArray jsonArray = jsonObject.optJSONArray(CHANNELS);
            ArrayList<ChannelNewBean> channelNewBeanArrayList = new ArrayList<>();
            for (int j = 0; j < jsonArray.length(); j++) {
                JSONObject object = jsonArray.getJSONObject(j);
                ChannelNewBean channelNewBean = new ChannelNewBean();
                channelNewBean.setId(object.optString(ID));
                channelNewBean.setName(object.optString(NAME));
                channelNewBean.setIcon(object.optString(ICON));
                channelNewBean.setType(object.optString(TYPE));
                JSONObject ob = object.optJSONObject(PARAM);
                ChannelNewParams params = new ChannelNewParams();
                if(ob != null){
                    params.setVt(ob.optString(VT));
                    params.setTitle(ob.optString(TITLE));
                    params.setArea(ob.optString(AREA));
                    params.setThemeId(ob.optString(THEME_ID));
                    params.setUrl(ob.optString(URL));
                    params.setOrderby(ob.optString(ORDERBY));
                    params.setYear(ob.optString(YEAR));
                    params.setSubcategory(ob.optString(SUBCATEGORY));
                    params.setAid(ob.optString(AID));
                }
                channelNewBean.setParam(params);
                channelNewBeanArrayList.add(channelNewBean);
            }
            channelPiece.setChannelNewBeanList(channelNewBeanArrayList);
            channelPieceArrayList.add(channelPiece);
        }
        channel.setChannel(channelPieceArrayList);
        return channel;
    }

    @Override
    protected JSONObject getData(String data) throws JSONException {
        mArr = new JSONArray(data);
        data = "{\"data\":\"data\"}";
        return super.getData(data);
    }

}
